package it.irondev.appturismo.dao;

import it.irondev.appturismo.dao.example.HikeExample;
import it.irondev.appturismo.dao.provider.HikeSqlProvider;
import it.irondev.appturismo.entity.Difficulty;
import it.irondev.appturismo.entity.Gallery;
import it.irondev.appturismo.entity.Hike;
import it.irondev.appturismo.entity.Language;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

public interface HikeMapper {
   
	@SelectProvider(type=HikeSqlProvider.class, method="countByExample")
    int countByExample(HikeExample example);

    @DeleteProvider(type=HikeSqlProvider.class, method="deleteByExample")
    int deleteByExample(HikeExample example);

    @Delete({
        "delete from Hike",
        "where idHike = #{idhike,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer idhike);

    @Insert({
        "insert into Hike (name,description,duration,web,",
        "difficulty,language)",
        "values (#{name,jdbcType=VARCHAR},#{description,jdbcType=VARCHAR},#{duration,jdbcType=VARCHAR},#{web,jdbcType=VARCHAR},",
        "#{difficulty.iddifficulty,jdbcType=INTEGER},#{language.idlanguage,jdbcType=INTEGER})"
    })
	@Options(useGeneratedKeys = true, keyProperty="idHike")
    int insert(Hike record);

    @InsertProvider(type=HikeSqlProvider.class, method="insertSelective")
    int insertSelective(Hike record);

    @SelectProvider(type=HikeSqlProvider.class, method="selectByExample")
    @Results({
        @Result(column="idHike", property="idhike", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
        @Result(column="duration", property="duration", jdbcType=JdbcType.VARCHAR),
        @Result(column="web", property="web", jdbcType=JdbcType.VARCHAR),
		@Result(column="gallery", property="gallery", javaType=Gallery.class, one=@One(select="it.irondev.appturismo.dao.GalleryMapper.selectByPrimaryKey")),
        @Result(column="difficulty", property="difficulty",  javaType=Difficulty.class, one=@One(select="it.irondev.appturismo.dao.DifficultyMapper.selectByPrimaryKey")),
		@Result(column="language", property="language", javaType=Language.class, one=@One(select="it.irondev.appturismo.dao.LanguageMapper.selectByPrimaryKey"))
    })
    List<Hike> selectByExample(HikeExample example);

    @Select({
        "select",
        "idHike, description, difficulty,name,language,gallery",
        "from Hike",
        "where idHike = #{idhike,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="idHike", property="idhike", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
        @Result(column="duration", property="duration", jdbcType=JdbcType.VARCHAR),
        @Result(column="web", property="web", jdbcType=JdbcType.VARCHAR),
        @Result(column="difficulty", property="difficulty",  javaType=Difficulty.class, one=@One(select="it.irondev.appturismo.dao.DifficultyMapper.selectByPrimaryKey")),
		@Result(column="gallery", property="gallery", javaType=Gallery.class, one=@One(select="it.irondev.appturismo.dao.GalleryMapper.selectByPrimaryKey")),
        @Result(column="language", property="language", javaType=Language.class, one=@One(select="it.irondev.appturismo.dao.LanguageMapper.selectByPrimaryKey"))
    })
    Hike selectByPrimaryKey(Integer idhike);

    @UpdateProvider(type=HikeSqlProvider.class, method="updateByExampleSelective")
    int updateByExampleSelective(@Param("record") Hike record, @Param("example") HikeExample example);

    @UpdateProvider(type=HikeSqlProvider.class, method="updateByExample")
    int updateByExample(@Param("record") Hike record, @Param("example") HikeExample example);

    @UpdateProvider(type=HikeSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Hike record);

    @Update({
        "update Hike",
        "set description = #{description,jdbcType=VARCHAR},",
         "name = #{name,jdbcType=VARCHAR},",
         "duration = #{duration,jdbcType=VARCHAR},",
         "difficulty = #{difficulty.iddifficulty,jdbcType=INTEGER},",
         "web = #{web,jdbcType=VARCHAR},",
//         "gallery = #{gallery.idgallery,jdbcType=INTEGER},",
         "language = #{language.idlanguage,jdbcType=INTEGER}",
        "where idHike = #{idhike,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Hike record);
}